<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <div>
      <div
        v-for="(item, index) in arr"
        style="display: inline-block"
      >
        <input v-model="isShow[index]" value="item" type="checkbox"  />
        <span>{{item}}</span>
      </div>
      <p>你选中的元素, 累加的值和为:{{sum}} </p>
    </div>
  </div>
  <script src="../vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [9, 15, 19, 25, 29, 31, 48, 57, 62, 79, 87],
        isShow:[false,false,false,false,false,false,false,false,false,false,false],
        sum:0,
      },
      watch: {
        isShow : {
          deep:true,
          handler(newValue) {
          console.log(newValue)
            this.arr.forEach((item, index) => {
              if (newValue[index] === true) {
                this.sum += item
                // console.log(item)
              }
            })
          }
        }
      },
      // computed: {
      //   sum() {
      //   return  this.arr.reduce((pre, item, index) => {
      //     if (this.isShow[index]) {
      //       return pre + item
      //     } 
      //   }, 0)
      //   }
      // }
    })
  </script>
</body>

</html>